#include "head.h"

// 菜单信息 - 登陆用
const wchar_t *title = L"蓝天控航管理系统";
const wchar_t *option_1[] = {L"登录", L"注册", L"退出程序"};

// 菜单信息 - 管理员用
const wchar_t *title_root = L"用户菜单";
const wchar_t *option_root[] = {
    L"设定航班",
    L"修改航班",
    L"增加超级用户",
    L"返回",
};

// 菜单信息 - 普通用户用
const wchar_t *title_user = L"用户菜单";
const wchar_t *option_user[] = {
    L"购票退票",
    L"余额充值",
    L"查看航班",
    L"修改密码",
    L"我的订单",
    L"返回",
};

// 菜单信息 - 修改航班信息用
const wchar_t *title_flight = L"航班可修改的信息";
const wchar_t *option_flight[] = {
    L"飞机机型",
    L"起点站和终点站",
    L"起飞时间和到达时间",
    L"飞行时间",
    L"票价",
    L"载客量",
    L"剩余座位",
    L"是否飞",
    L"返回"};

const wchar_t *title_ticket = L"航班可修改的信息";
const wchar_t *option_ticket[] = {
    L"购票",
    L"退票",
    L"返回",
};

// 系统初始化
FlightSystem Sys_init(void)
{
    FlightSystem Flight_sys;
    Flight_sys.Flight_list = D_List_init();   // 航班信息
    Flight_sys.ADMIN_USER = S_List_init();    // 管理员账户信息
    Flight_sys.REGULAR_USER = S_List_init();  // 普通用户信息
    Flight_sys.SILVER_USER = S_List_init();   // 银卡会员信息 (9折)
    Flight_sys.GOLD_USER = S_List_init();     // 金卡会员信息 (8折)
    Flight_sys.PLATINUM_USER = S_List_init(); // 白金会员信息 (7折)

    // 从文件中导入数据
    D_List_file_scanf(Flight_sys.Flight_list);                  // 航班信息
    S_List_File_scanf(Flight_sys.ADMIN_USER, ADMIN_FILE);       // 管理员账户信息文件
    S_List_File_scanf(Flight_sys.REGULAR_USER, REGULAR_FILE);   // 普通用户信息文件
    S_List_File_scanf(Flight_sys.SILVER_USER, SILVER_FILE);     // 银卡会员信息文件 (9折)
    S_List_File_scanf(Flight_sys.GOLD_USER, GOLD_FILE);         // 金卡会员信息文件 (8折)
    S_List_File_scanf(Flight_sys.PLATINUM_USER, PLATINUM_FILE); // 白金会员信息文件 (7折)

    system("clear");
    return Flight_sys;
}

// 注册
void reg(U_list *list)
{
    int flag_1 = 1;
    int num = 0;
    char buf_pwd[128];
    U_data reg_data;
    reg_data.grade = GRADE_REGULAR;
    reg_data.value = 0;
    reg_data.recharge = 0;
    reg_data.order=0;
    U_node *ptr = list->next;
    for (size_t i = 0; i < ptr->data.order; i++)
    {
        reg_data.ticket[i] = ptr->data.ticket[i];
    }
    printf("注册界面\n");
    while (flag_1)
    {
        printf("你的账号:");
        scanf("%s", reg_data.name);
        printf("你的密码:");
        scanf("%s", reg_data.passwd);
        printf("请再次输入你的密码:");
        scanf("%s", buf_pwd);
        if (!strcmp(reg_data.passwd, buf_pwd))
        {
            printf("恭喜你 注册成功\n");
            flag_1 = 0;
        }
        else
        {
            printf("密码与上述输入不相同,请5s后重新开始注册\n");
            sleep(5);
            num++;
        }

        if (num == 5)
        {
            printf("注册失误5次,返回主菜单\n");
            flag_1 = 0;
        }
    }
    tail_insert_node(list, reg_data);
    S_List_File_print(list, REGULAR_FILE);
}

// 主菜单
void MENU(FlightSystem Flight_Sys)
{
    int flag_m = 0;
    U_data user_data;
    flag_m = display_menu(title, option_1, 3);
    switch (flag_m)
    {
    case 1:
        // 进入登陆操作
        user_data = Sys_log_in(Flight_Sys);
        // 进入菜单
        Sys_menu(Flight_Sys, user_data);
        break;
    case 2:
        reg(Flight_Sys.REGULAR_USER);
        break;
    case 3:
        exit(-1);
        break;
    default:
        break;
    }
}

// 系统登录
U_data Sys_log_in(FlightSystem Flight_sys)
{
    // 用户信息合表
    U_list *list = S_List_init();
    list = S_List_merge(list, Flight_sys.ADMIN_USER);    // 管理员账户信息文件
    list = S_List_merge(list, Flight_sys.REGULAR_USER);  // 普通用户信息文件
    list = S_List_merge(list, Flight_sys.SILVER_USER);   // 银卡会员信息文件 (9折)
    list = S_List_merge(list, Flight_sys.GOLD_USER);     // 金卡会员信息文件 (8折)
    list = S_List_merge(list, Flight_sys.PLATINUM_USER); // 白金会员信息文件 (7折)

    // 开始登录
    U_data log_data = Log_in(list);
    return log_data;
}

// 系统菜单
void Sys_menu(FlightSystem Flight_Sys, U_data log_data)
{
    int flag = 0;
    int flag_1 = 1;
    while (flag_1)
    {
        F_list *Fptr = Flight_Sys.Flight_list;       // 航班信息
        U_list *admi_ptr = Flight_Sys.ADMIN_USER;    // 管理员账户信息
        U_list *comm_ptr = Flight_Sys.REGULAR_USER;  // 普通用户信息
        U_list *silv_ptr = Flight_Sys.SILVER_USER;   // 银卡会员信息 (9折)
        U_list *gold_ptr = Flight_Sys.GOLD_USER;     // 金卡会员信息 (8折)
        U_list *plat_ptr = Flight_Sys.PLATINUM_USER; // 白金会员信息 (7折)
        switch (log_data.grade)
        {
        case 1:
            // 管理员功能界面
            printf("欢迎管理员用户登陆\n");
            flag = display_menu(title_root, option_root, 4);
            break;
        default:
            // 普通用户功能界面
            printf("欢迎普通用户登陆\n");
            flag = display_menu(title_user, option_user, 6) + 4;
            break;
        }
        // 菜单选项分支处理
        switch (flag)
        {
        case 1:
            // 管理员 - 设定航班
            set_flight(Fptr, log_data);
            break;
        case 2:
            // 管理员 - 修改航班
            change_flight(Fptr, log_data);
            break;
        case 3:
            // 管理员 - 增加超级用户（管理员）
            add_admim(admi_ptr, log_data);
            break;
        case 4:
            // 管理员 - 返回登陆
            flag_1 = 0;
            break;
        case 5:
            // 用户 - 购票退票
            if (GRADE_ADMIN == log_data.grade)
            {
                ticket_transaction(Fptr, admi_ptr, log_data, ADMIN_FILE);
            }
            else if (GRADE_REGULAR == log_data.grade)
            {
                ticket_transaction(Fptr, comm_ptr, log_data, REGULAR_FILE);
            }
            else if (GRADE_SILVER == log_data.grade)
            {
                ticket_transaction(Fptr, silv_ptr, log_data, SILVER_FILE);
            }
            else if (GRADE_GOLD == log_data.grade)
            {
                ticket_transaction(Fptr, gold_ptr, log_data, GOLD_FILE);
            }
            else if (GRADE_PLATINUM == log_data.grade)
            {
                ticket_transaction(Fptr, plat_ptr, log_data, PLATINUM_FILE);
            }
            break;
        case 6:
            // 用户 - 余额充值
            if (GRADE_ADMIN == log_data.grade)
            {
                balance_recharge(Flight_Sys, admi_ptr, log_data, ADMIN_FILE);
            }
            else if (GRADE_REGULAR == log_data.grade)
            {
                balance_recharge(Flight_Sys, comm_ptr, log_data, REGULAR_FILE);
            }
            else if (GRADE_SILVER == log_data.grade)
            {
                balance_recharge(Flight_Sys, silv_ptr, log_data, SILVER_FILE);
            }
            else if (GRADE_GOLD == log_data.grade)
            {
                balance_recharge(Flight_Sys, gold_ptr, log_data, GOLD_FILE);
            }
            else if (GRADE_PLATINUM == log_data.grade)
            {
                balance_recharge(Flight_Sys, plat_ptr, log_data, PLATINUM_FILE);
            }
            break;
        case 7:
            // 用户 - 查看航班
            check_flights(Fptr, log_data);
            break;
        case 8:
            // 用户 - 修改密码
            if (GRADE_ADMIN == log_data.grade)
            {
                change_pwd(admi_ptr, log_data, ADMIN_FILE);
            }
            else if (GRADE_REGULAR == log_data.grade)
            {
                change_pwd(comm_ptr, log_data, REGULAR_FILE);
            }
            else if (GRADE_SILVER == log_data.grade)
            {
                change_pwd(silv_ptr, log_data, SILVER_FILE);
            }
            else if (GRADE_GOLD == log_data.grade)
            {
                change_pwd(gold_ptr, log_data, GOLD_FILE);
            }
            else if (GRADE_PLATINUM == log_data.grade)
            {
                change_pwd(plat_ptr, log_data, PLATINUM_FILE);
            }
            break;
        case 9:
            // 用户 - 我的订单
            if (GRADE_ADMIN == log_data.grade)
            {
                find_order(admi_ptr, log_data);
            }
            else if (GRADE_REGULAR == log_data.grade)
            {
                find_order(comm_ptr, log_data);
            }
            else if (GRADE_SILVER == log_data.grade)
            {
                find_order(silv_ptr, log_data);
            }
            else if (GRADE_GOLD == log_data.grade)
            {
                find_order(gold_ptr, log_data);
            }
            else if (GRADE_PLATINUM == log_data.grade)
            {
                find_order(plat_ptr, log_data);
            }
            sleep(5);
            break;
        case 10:
            // 返回 登陆
            flag_1 = 0;
            break;
        default:
            break;
        }
    }
}

// 管理员 设定航班
void set_flight(F_list *Flight_list, U_data log_data)
{
    int index = 0;
    // F_list* copyList(F_list *head)

    F_node *p1 = Flight_list->next;
    F_node *p2 = p1->next->prev;
    do
    {
        index++;
        p2 = p2->next;
    } while (p1 != p2);

    int num = 1;
    F_data set_data;
    FILE *fp = fopen(FLIGHTS_FILE, "r+");
    if (NULL == fp)
    {
        num = 1;
    }
    char ch = 0;
    // 检测文件行数
    while (EOF != (ch = fgetc(fp)))
    {
        if ('\n' == ch)
        {
            num++;
        }
    }
    set_data.id = num; // 航班班次
    printf("飞机机型:\n");
    scanf("%s", set_data.flighttype); // 飞机机型
    printf("航班起点站:\n");
    scanf("%s", set_data.startcity); // 航班起点站
    printf("航班终点站:\n");
    scanf("%s", set_data.arrivecity); // 航班终点站
    printf("起飞时间:\n");
    scanf("%02d %02d", &set_data.starttime[0], &set_data.starttime[1]); // 起飞时间 时:分
    printf("到达时间:\n");
    scanf("%02d %02d", &set_data.arrivetime[0], &set_data.arrivetime[1]); // 到达时间 时:分
    printf("飞行时间:\n");
    scanf("%s", set_data.flytime); // 飞行时间
    printf("票价:\n");
    scanf("%d", &set_data.value); // 票价
    printf("额定载客量:\n");
    scanf("%d", &set_data.maxNUM); // 额定载客量
    printf("剩余座位数量:\n");
    scanf("%d", &set_data.leftbuyersa); // 剩余座位数量
    printf("是否起飞:\n");
    scanf("%s", &set_data.whetherfly); // 是否起飞 ‘y’ ‘n’
    D_List_insert(Flight_list, set_data, index + 1);
    return;
}

// 管理员 - 修改航班
void change_flight(F_list *Flight_list, U_data log_data)
{
    int flag = 0;
    int id_1 = 0;
    D_List_print(Flight_list);
    printf("请选择你要修改的航班的班次:");
    scanf("%d", &id_1);
    F_node *ptr = D_List_find_id(Flight_list, id_1);
    flag = display_menu(title_flight, option_flight, 8);
    switch (flag)
    {
    case 1:
        // 飞机机型
        change(Flight_list, log_data, ptr, flag);
        break;
    case 2:
        // 起点站和终点站
        change(Flight_list, log_data, ptr, flag);
        break;
    case 3:
        // 起飞时间和到达时间
        change(Flight_list, log_data, ptr, flag);
        break;
    case 4:
        // 飞行时间
        change(Flight_list, log_data, ptr, flag);
        break;
    case 5:
        // 票价
        change(Flight_list, log_data, ptr, flag);
        break;
    case 6:
        // 载客量
        change(Flight_list, log_data, ptr, flag);
        break;
    case 7:
        // 剩余座位
        change(Flight_list, log_data, ptr, flag);
        break;
    case 8:
        // 是否飞
        change(Flight_list, log_data, ptr, flag);
        break;
    default:
        break;
    }
}

// 管理员增加超级用户
void add_admim(U_list *user_list, U_data log_data)
{
    U_data add_data;
    printf("用户名\n");
    scanf("%s", add_data.name);
    printf("用户密码\n");
    scanf("%s", add_data.passwd);
    add_data.grade = GRADE_ADMIN;
    add_data.value = 0;
    add_data.recharge = 0;
    add_data.order = 0;
    for (size_t i = 0; i < 10; i++)
    {
        add_data.ticket[i].id = '\0';
        add_data.ticket[i].flighttype[20] = '\0';
        add_data.ticket[i].startcity[20] = '\0';
        add_data.ticket[i].arrivecity[20] = '\0';
        add_data.ticket[i].starttime[2] = '\0';
        add_data.ticket[i].arrivetime[2] = '\0';
        add_data.ticket[i].value = '\0';
        add_data.ticket[i].maxNUM = '\0';
        add_data.ticket[i].leftbuyersa = '\0';
        add_data.ticket[i].whetherfly = '\0';
    }
    S_List_insert(user_list, add_data, 1);
    S_List_File_print(user_list, ADMIN_FILE);
}

// 用户 购票退票
void ticket_transaction(F_list *Flight_list, U_list *user_list, U_data log_data, const char *file_name)
{
    int flag_k = 0;
    flag_k = display_menu(title_ticket, option_ticket, 3);
    // F_list *cpy_Flight_list=copyList(Flight_list);
    switch (flag_k)
    {
    case 1:
        buy_ticket(Flight_list, user_list, log_data, file_name);
        break;
    case 2:
        // 退票
        refund_ticket(Flight_list, user_list, log_data, file_name);
        break;
    case 3:
        // 返回
        break;
    default:
        break;
    }
}

// 用户 - 余额充值
void balance_recharge(FlightSystem Flight_Sys, U_list *user_list, U_data log_data, const char *file_name)
{
    U_node *ptr = S_List_find_name(user_list, log_data.name);
    int flag = 0;
    int pos = 0;
    U_data ba_data;

    int rech = 0;
    int rech_1 = 0;
    printf("请输入你要充值的金额:");
    scanf("%d", &rech);
    ba_data.recharge = rech + ptr->data.recharge; // 累计充值
    rech_1 = ba_data.recharge;
    ba_data.order = ptr->data.order;
    strcpy(ba_data.name, ptr->data.name);
    strcpy(ba_data.passwd, ptr->data.passwd);
    for (size_t i = 0; i < sizeof(ptr->data.ticket) / sizeof(ptr->data.ticket[0]); i++)
    {
        ba_data.ticket[i] = ptr->data.ticket[i];
    }
    U_node *ptr_1 = user_list->next;
    do
    {
        if (strcmp(ptr_1->data.name, log_data.name) == 0)
        {
            flag = 1;
        }
        pos++;
        ptr_1 = ptr_1->next;
    } while (ptr != NULL && 0 == flag);
    ba_data.value = ptr->data.value + rech; // 余额
    if (GRADE_ADMIN != log_data.grade)
    {
        if (rech_1 < 10000)
        {
            ba_data.grade = GRADE_REGULAR;
            S_List_delete(user_list, pos);
            S_List_insert(Flight_Sys.REGULAR_USER, ba_data, 1);
            S_List_File_print(user_list, file_name);
            S_List_File_print(Flight_Sys.REGULAR_USER, REGULAR_FILE);
        }
        if (rech_1 >= 10000 && rech_1 < 15000)
        {
            ba_data.grade = GRADE_SILVER;
            S_List_delete(user_list, pos);
            S_List_insert(Flight_Sys.SILVER_USER, ba_data, 1);
            S_List_File_print(user_list, file_name);
            S_List_File_print(Flight_Sys.SILVER_USER, SILVER_FILE);
        }
        if (rech_1 >= 15000 && rech_1 < 20000)
        {
            ba_data.grade = GRADE_GOLD;
            S_List_delete(user_list, pos);
            S_List_insert(Flight_Sys.GOLD_USER, ba_data, 1);
            S_List_File_print(user_list, file_name);
            S_List_File_print(Flight_Sys.GOLD_USER, GOLD_FILE);
        }
        if (rech_1 >= 20000)
        {
            ba_data.grade = GRADE_PLATINUM;
            S_List_delete(user_list, pos);
            S_List_insert(Flight_Sys.PLATINUM_USER, ba_data, pos);
            S_List_File_print(user_list, file_name);
            S_List_File_print(Flight_Sys.PLATINUM_USER, PLATINUM_FILE);
        }
    }
    else
    {
        S_List_delete(user_list, pos);
        S_List_insert(user_list, ba_data, pos);
        S_List_File_print(user_list, file_name);
    }
    if (NULL == S_List_find_name(user_list, log_data.name))
    {
        printf("您已成功升级会员,请前往重新登陆.\n");
        sleep(5);
    }
}

// 用户 密码修改
void change_pwd(U_list *user_list, U_data log_data, const char *file_name)
{
    system("clear");
    char buf_pwd[128];
    int flag_1 = 1;
    int flag_2 = 1;
    int flag_3 = 0;
    int num = 0;
    int pos = 0;
    U_data data;
    while (flag_1)
    {
        printf("修改密码\n");
        printf("你的用户名:%s\n", log_data.name);
        // scanf("%s", buf_name);
        U_node *ptr = S_List_find_name(user_list, log_data.name);
        data.grade = ptr->data.grade;
        data.value = ptr->data.value;
        data.recharge = ptr->data.recharge;
        data.order = ptr->data.order;
        for (size_t i = 0; i < ptr->data.order; i++)
        {
            data.ticket[i] = ptr->data.ticket[i];
        }
        printf("你的原始密码:");
        scanf("%s", data.passwd);
        if (NULL == ptr)
        {
            printf("用户名不存在,请重试\n");
        }
        else if (!strcmp(ptr->data.passwd, data.passwd))
        {
            num++;
            while (flag_2)
            {
                printf("你的新密码:\n");
                scanf("%s", data.passwd);
                if (!strcmp(ptr->data.passwd, data.passwd))
                {
                    printf("新密码与旧密码相同,请5s后重新输入\n");
                    sleep(5);
                    system("clear");
                    num++;
                }
                else
                {
                    printf("请再次输入你的新密码:\n");
                    scanf("%s", buf_pwd);
                    if (!strcmp(buf_pwd, data.passwd))
                    {
                        printf("密码修改成功\n");
                        strcpy(data.name, log_data.name);
                        flag_1 = 0;
                        flag_2 = 0;
                    }
                    else
                    {
                        printf("密码与上述输入不相同,请5s后重新输入\n");
                        num++;
                    }
                }
                if (5 == num)
                {
                    printf("修改密码5次失败,请稍后再试");
                    flag_1 = 0;
                    flag_2 = 0;
                }
            }
        }
    }
    if (5 == num)
    {
        return;
    }
    U_node *ptr = user_list->next;
    do
    {
        if (strcmp(ptr->data.name, log_data.name) == 0)
        {
            flag_3 = 1;
        }
        pos++;
        ptr = ptr->next;
    } while (ptr != NULL && 0 == flag_3);
    S_List_delete(user_list, pos);
    S_List_insert(user_list, data, pos);
    S_List_File_print(user_list, file_name);
    fflush(stdout);
}

// 用户 查看航班
void check_flights(F_list *Flight_list, U_data log_data)
{
    system("clear");
    D_List_print(Flight_list);
    return;
}

// 购票
void buy_ticket(F_list *Flight_list, U_list *user_list, U_data log_data, const char *file_name)
{
    system("clear");
    // 购票的定义
    F_node *ptr_1 = Flight_list->next;
    F_node *ptr_2 = ptr_1->next;
    char buf_name[128]; // 终点站
    int shift = 0;      // 班次
    int pos = 0;
    U_node *u_ptr = user_list->next;
    U_node *ptr = S_List_find_name(user_list, log_data.name);
    int flag = 0;
    U_data t_data;
    F_data buy_data;
    printf("终点站:");
    scanf("%s", buf_name);
    F_node *buy_ptr = D_List_find_name(Flight_list, buf_name);
    // 打印所有到达该终点站的班次
    printf("班次    机型        起点站  终点站  起飞时间    到达时间    飞行时间    票价  载客量  剩余座位  是否飞\n");
    printf("--------------------------------------------------------------------------------------------------\n");
    do
    {
        if (!strcmp(ptr_1->data.arrivecity, buf_name))
        {
            printf("%-6d", ptr_1->data.id);
            printf("%-15s", ptr_1->data.flighttype);
            printf("%-10s", ptr_1->data.startcity);
            printf("%-10s", ptr_1->data.arrivecity);
            printf("%02d:%02d\t", ptr_1->data.starttime[0], ptr_1->data.starttime[1]);
            printf("%02d:%02d\t    ", ptr_1->data.arrivetime[0], ptr_1->data.arrivetime[1]);
            printf("%-10s\t", ptr_1->data.flytime);
            printf("%-6d", ptr_1->data.value);
            printf("%-8d", ptr_1->data.maxNUM);
            printf("%-8d  ", ptr_1->data.leftbuyersa);
            printf("%c\n", ptr_1->data.whetherfly);
            // z++;
        }
        ptr_1 = ptr_1->next;
        ptr_2 = ptr_2->next;
    } while (ptr_1 != Flight_list->next);
    printf("请选择你要买的班次");
    scanf("%d", &shift);
    // 写入更新后的航班信息
    buy_data.id = buy_ptr->data.id;                        // 航班班次
    strcpy(buy_data.flighttype, buy_ptr->data.flighttype); // 飞机机型
    strcpy(buy_data.startcity, buy_ptr->data.startcity);   // 起点站
    strcpy(buy_data.arrivecity, buy_ptr->data.arrivecity); // 终点站
    for (size_t i = 0; i < 2; i++)                         // 起飞时间
    {
        buy_data.starttime[i] = buy_ptr->data.starttime[i];
    }
    for (size_t i = 0; i < 2; i++) // 到达时间
    {
        buy_data.arrivetime[i] = buy_ptr->data.arrivetime[i];
    }
    strcpy(buy_data.flytime, buy_ptr->data.flytime);      // 飞行时间
    buy_data.value = buy_ptr->data.value;                 // 票价
    buy_data.maxNUM = buy_ptr->data.maxNUM;               // 额定载客量
    buy_data.leftbuyersa = buy_ptr->data.leftbuyersa - 1; // 剩余座位数量
    buy_data.whetherfly = buy_ptr->data.whetherfly;       // 是否起飞
    D_List_delete(Flight_list, shift);
    D_List_insert(Flight_list, buy_data, shift);
    D_List_file_print(Flight_list);

    // 写入更新后的用户信息
    strcpy(t_data.name, ptr->data.name);
    strcpy(t_data.passwd, ptr->data.passwd);
    t_data.grade = ptr->data.grade;
    if (GRADE_REGULAR == ptr->data.grade)
    {
        t_data.value = ptr->data.value - buy_data.value;
    }
    if (GRADE_SILVER == ptr->data.grade)
    {
        t_data.value = ptr->data.value - buy_data.value * 0.9;
    }
    if (GRADE_GOLD == ptr->data.grade)
    {
        t_data.value = ptr->data.value - buy_data.value * 0.8;
    }
    if (GRADE_PLATINUM == ptr->data.grade)
    {
        t_data.value = ptr->data.value - buy_data.value * 0.7;
    }
    t_data.recharge = ptr->data.recharge;

    t_data.order = ptr->data.order + 1; // 订单数

    int len = 0;
    len = ptr->data.order;
    for (int j = 0; j < len; j++)
    {
        t_data.ticket[j] = ptr->data.ticket[j];
    }
    t_data.ticket[len] = buy_data;

    do
    {
        if (0 == strcmp(u_ptr->data.name, log_data.name))
        {
            flag = 1;
        }
        u_ptr = u_ptr->next;
        pos++;
    } while (u_ptr != NULL && 0 == flag);
    S_List_delete(user_list, pos);
    S_List_insert(user_list, t_data, pos);
    S_List_File_print(user_list, file_name);
}

// 退票
void refund_ticket(F_list *Flight_list, U_list *user_list, U_data log_data, const char *file_name)
{
    int shift = 0;      // 退票班次
    U_data t_data;      // 用户信息
    F_data refund_data; // 航班信息
    printf("你的所有车票如下:");
    find_order(user_list, log_data);
    printf("你要退票的班次:");
    scanf("%d", &shift);
    F_node *refund_ptr = D_List_find_id(Flight_list, shift);

    // 更新航班信息
    refund_data.id = refund_ptr->data.id;                        // 航班班次
    strcpy(refund_data.flighttype, refund_ptr->data.flighttype); // 飞机机型
    strcpy(refund_data.startcity, refund_ptr->data.startcity);   // 起点站
    strcpy(refund_data.arrivecity, refund_ptr->data.arrivecity); // 终点站
    for (size_t i = 0; i < 2; i++)                               // 起飞时间
    {
        refund_data.starttime[i] = refund_ptr->data.starttime[i];
    }
    for (size_t i = 0; i < 2; i++) // 到达时间
    {
        refund_data.arrivetime[i] = refund_ptr->data.arrivetime[i];
    }
    strcpy(refund_data.flytime, refund_ptr->data.flytime);      // 飞行时间
    refund_data.value = refund_ptr->data.value;                 // 票价
    refund_data.maxNUM = refund_ptr->data.maxNUM;               // 额定载客量
    refund_data.leftbuyersa = refund_ptr->data.leftbuyersa + 1; // 剩余座位数量
    refund_data.whetherfly = refund_ptr->data.whetherfly;       // 是否起飞
    D_List_delete(Flight_list, shift);
    D_List_insert(Flight_list, refund_data, shift);
    D_List_file_print(Flight_list);

    int flag = 0;
    int pos = 0;
    U_node *u_ptr = user_list->next;
    U_node *ptr = S_List_find_name(user_list, log_data.name);
    // 写入更新后的用户信息
    strcpy(t_data.name, ptr->data.name);
    strcpy(t_data.passwd, ptr->data.passwd);
    t_data.grade = ptr->data.grade;
    t_data.value = ptr->data.value; // 退款前的金额

    if (GRADE_REGULAR == ptr->data.grade)
    {
        t_data.value = ptr->data.value + refund_data.value;
    }
    if (GRADE_SILVER == ptr->data.grade)
    {
        t_data.value = ptr->data.value + refund_data.value * 0.9;
    }
    if (GRADE_GOLD == ptr->data.grade)
    {
        t_data.value = ptr->data.value + refund_data.value * 0.8;
    }
    if (GRADE_PLATINUM == ptr->data.grade)
    {
        t_data.value = ptr->data.value + refund_data.value * 0.7;
    }
    t_data.recharge = ptr->data.recharge;

    t_data.order = ptr->data.order - 1; // 订单数减1

    int len = ptr->data.order;
    for (int j = 0; j < len - 1; j++)
    {
        t_data.ticket[j] = ptr->data.ticket[j];
    }

    do
    {
        if (0 == strcmp(u_ptr->data.name, log_data.name))
        {
            flag = 1;
        }
        u_ptr = u_ptr->next;
        pos++;
    } while (u_ptr != NULL && 0 == flag);
    S_List_delete(user_list, pos);
    S_List_insert(user_list, t_data, pos);
    S_List_File_print(user_list, file_name);
}

// 查看订单
void find_order(U_list *user_list, U_data log_data)
{
    system("clear");
    U_node *ptr = S_List_find_name(user_list, log_data.name);
    printf("姓名:%s\t", ptr->data.name);
    if (GRADE_ADMIN == ptr->data.grade)
    {
        printf("等级:管理员\n");
        printf("管理员不能参与购票\n");
        sleep(5);
    }
    if (GRADE_REGULAR == ptr->data.grade)
    {
        printf("等级:普通用户\n");
    }
    if (GRADE_SILVER == ptr->data.grade)
    {
        printf("等级:银卡会员\n");
    }
    if (GRADE_GOLD == ptr->data.grade)
    {
        printf("等级:金卡会员\n");
    }
    if (GRADE_PLATINUM == ptr->data.grade)
    {
        printf("等级:白金会员\n");
    }
    for (size_t i = 0; i < ptr->data.order; i++)
    {
        printf("班次:%-6d\t机型:%-15s\t起点:%-10s\t终点:%-10s\t起飞时间:%02d:%02d\t到达时间:%02d:%02d\t飞行时间:%-10s\t票价:%-6d\t额定载客量:%-8d\n",
               ptr->data.ticket[i].id,
               ptr->data.ticket[i].flighttype,
               ptr->data.ticket[i].startcity,
               ptr->data.ticket[i].arrivecity,
               ptr->data.ticket[i].starttime[0], ptr->data.ticket[i].starttime[1],
               ptr->data.ticket[i].arrivetime[0], ptr->data.ticket[i].arrivetime[1],
               ptr->data.ticket[i].flytime,
               ptr->data.ticket[i].value,
               ptr->data.ticket[i].maxNUM);
    }
}

// 修改航班信息分支 修改航班单条信息
void change(F_list *Flight_list, U_data log_data, F_node *ptr, int flag)
{
    F_data ch_data;
    int ch_id = ptr->data.id;
    // char f;
    int flag_city = 1;
    switch (flag)
    {
    case 1:
        // 飞机机型
        D_List_print(Flight_list);
        printf("原飞机机型:%s\n", ptr->data.flighttype);
        printf("新机型:");
        scanf("%s", ch_data.flighttype);
        ch_data.id = ptr->data.id;                        // 航班班次
        strcpy(ch_data.startcity, ptr->data.startcity);   // 起点站
        strcpy(ch_data.arrivecity, ptr->data.arrivecity); // 终点站
        for (size_t i = 0; i < 2; i++)                    // 起飞时间
        {
            ch_data.starttime[i] = ptr->data.starttime[i];
        }
        for (size_t i = 0; i < 2; i++) // 到达时间
        {
            ch_data.arrivetime[i] = ptr->data.arrivetime[i];
        }
        strcpy(ch_data.flytime, ptr->data.flytime);  // 飞行时间
        ch_data.value = ptr->data.value;             // 票价
        ch_data.maxNUM = ptr->data.maxNUM;           // 额定载客量
        ch_data.leftbuyersa = ptr->data.leftbuyersa; // 剩余座位数量
        ch_data.whetherfly = ptr->data.whetherfly;   // 是否起飞
        D_List_delete(Flight_list, ch_id);
        D_List_insert(Flight_list, ch_data, ch_id);
        D_List_file_print(Flight_list);
        break;
    case 2:
        // 起点站和终点站
        D_List_print(Flight_list);
        printf("原起点站:%s\n原终点站:%s\n", ptr->data.startcity, ptr->data.arrivecity);
        while (flag_city)
        {
            printf("现起点站:");
            scanf("%s", ch_data.startcity);
            printf("现终点站:");
            scanf("%s", ch_data.arrivecity);
            if (!strcmp(ch_data.startcity, ch_data.arrivecity))
            {
                printf("起点站和终点站相同,请重新输入\n");
            }
            else
            {
                flag_city = 0;
            }
        }
        ch_data.id = ptr->data.id; // 航班班次
        strcpy(ch_data.flighttype, ptr->data.flighttype);
        for (size_t i = 0; i < 2; i++) // 起飞时间
        {
            ch_data.starttime[i] = ptr->data.starttime[i];
        }
        for (size_t i = 0; i < 2; i++) // 到达时间
        {
            ch_data.arrivetime[i] = ptr->data.arrivetime[i];
        }
        strcpy(ch_data.flytime, ptr->data.flytime);  // 飞行时间
        ch_data.value = ptr->data.value;             // 票价
        ch_data.maxNUM = ptr->data.maxNUM;           // 额定载客量
        ch_data.leftbuyersa = ptr->data.leftbuyersa; // 剩余座位数量
        ch_data.whetherfly = ptr->data.whetherfly;   // 是否起飞
        D_List_delete(Flight_list, ch_id);
        D_List_insert(Flight_list, ch_data, ch_id);
        D_List_file_print(Flight_list);
        break;
    case 3:
        // 起飞时间和到达时间
        D_List_print(Flight_list);
        printf("原起飞时间(时:分):%d:%d\n原到达时间:%d:%d\n", ptr->data.starttime[0], ptr->data.starttime[1], ptr->data.arrivetime[0], ptr->data.arrivetime[1]);
        printf("现起飞时间(时 分):");
        scanf("%d %d", &ch_data.starttime[0], &ch_data.starttime[1]);
        printf("现到达时间(时 分):");
        scanf("%d %d", &ch_data.arrivetime[0], &ch_data.arrivetime[1]);
        ch_data.id = ptr->data.id;                        // 航班班次
        strcpy(ch_data.flighttype, ptr->data.flighttype); // 飞机机型
        strcpy(ch_data.startcity, ptr->data.startcity);   // 起点站
        strcpy(ch_data.arrivecity, ptr->data.arrivecity); // 终点站
        for (size_t i = 0; i < 2; i++)                    // 起飞时间
        {
            ch_data.starttime[i] = ptr->data.starttime[i];
        }
        for (size_t i = 0; i < 2; i++) // 到达时间
        {
            ch_data.arrivetime[i] = ptr->data.arrivetime[i];
        }
        strcpy(ch_data.flytime, ptr->data.flytime);  // 飞行时间
        ch_data.value = ptr->data.value;             // 票价
        ch_data.maxNUM = ptr->data.maxNUM;           // 额定载客量
        ch_data.leftbuyersa = ptr->data.leftbuyersa; // 剩余座位数量
        ch_data.whetherfly = ptr->data.whetherfly;   // 是否起飞
        D_List_delete(Flight_list, ch_id);
        D_List_insert(Flight_list, ch_data, ch_id);
        D_List_file_print(Flight_list);
        break;
    case 4:
        // 飞行时间
        D_List_print(Flight_list);
        printf("原飞行时间:%s\n", ptr->data.flytime);
        printf("现飞行时间:");
        scanf("%s", ch_data.flytime);
        ch_data.id = ptr->data.id;                        // 航班班次
        strcpy(ch_data.flighttype, ptr->data.flighttype); // 飞机机型
        strcpy(ch_data.startcity, ptr->data.startcity);   // 起点站
        strcpy(ch_data.arrivecity, ptr->data.arrivecity); // 终点站
        for (size_t i = 0; i < 2; i++)                    // 起飞时间
        {
            ch_data.starttime[i] = ptr->data.starttime[i];
        }
        for (size_t i = 0; i < 2; i++) // 到达时间
        {
            ch_data.arrivetime[i] = ptr->data.arrivetime[i];
        }
        ch_data.value = ptr->data.value;             // 票价
        ch_data.maxNUM = ptr->data.maxNUM;           // 额定载客量
        ch_data.leftbuyersa = ptr->data.leftbuyersa; // 剩余座位数量
        ch_data.whetherfly = ptr->data.whetherfly;   // 是否起飞
        D_List_delete(Flight_list, ch_id);
        D_List_insert(Flight_list, ch_data, ch_id);
        D_List_file_print(Flight_list);
        break;
    case 5:
        // 票价
        D_List_print(Flight_list);
        printf("原票价:%d\n", ptr->data.value);
        printf("现票价:");
        scanf("%d", &ch_data.value);
        ch_data.id = ptr->data.id;                        // 航班班次
        strcpy(ch_data.flighttype, ptr->data.flighttype); // 飞机机型
        strcpy(ch_data.startcity, ptr->data.startcity);   // 起点站
        strcpy(ch_data.arrivecity, ptr->data.arrivecity); // 终点站
        for (size_t i = 0; i < 2; i++)                    // 起飞时间
        {
            ch_data.starttime[i] = ptr->data.starttime[i];
        }
        for (size_t i = 0; i < 2; i++) // 到达时间
        {
            ch_data.arrivetime[i] = ptr->data.arrivetime[i];
        }
        strcpy(ch_data.flytime, ptr->data.flytime);  // 飞行时间
        ch_data.maxNUM = ptr->data.maxNUM;           // 额定载客量
        ch_data.leftbuyersa = ptr->data.leftbuyersa; // 剩余座位数量
        ch_data.whetherfly = ptr->data.whetherfly;   // 是否起飞
        D_List_delete(Flight_list, ch_id);
        D_List_insert(Flight_list, ch_data, ch_id);
        D_List_file_print(Flight_list);
        break;
    case 6:
        // 载客量
        D_List_print(Flight_list);
        printf("原载客量:%d\n", ptr->data.maxNUM);
        printf("现载客量:");
        scanf("%d", &ch_data.maxNUM);
        ch_data.id = ptr->data.id;                        // 航班班次
        strcpy(ch_data.flighttype, ptr->data.flighttype); // 飞机机型
        strcpy(ch_data.startcity, ptr->data.startcity);   // 起点站
        strcpy(ch_data.arrivecity, ptr->data.arrivecity); // 终点站
        for (size_t i = 0; i < 2; i++)                    // 起飞时间
        {
            ch_data.starttime[i] = ptr->data.starttime[i];
        }
        for (size_t i = 0; i < 2; i++) // 到达时间
        {
            ch_data.arrivetime[i] = ptr->data.arrivetime[i];
        }
        strcpy(ch_data.flytime, ptr->data.flytime);  // 飞行时间
        ch_data.value = ptr->data.value;             // 票价
        ch_data.leftbuyersa = ptr->data.leftbuyersa; // 剩余座位数量
        ch_data.whetherfly = ptr->data.whetherfly;   // 是否起飞
        D_List_delete(Flight_list, ch_id);
        D_List_insert(Flight_list, ch_data, ch_id);
        D_List_file_print(Flight_list);
        break;
    case 7:
        // 剩余座位
        D_List_print(Flight_list);
        printf("原剩余座位:%d\n", ptr->data.leftbuyersa);
        printf("现剩余座位:");
        scanf("%d", &ch_data.leftbuyersa);
        ch_data.id = ptr->data.id;                        // 航班班次
        strcpy(ch_data.flighttype, ptr->data.flighttype); // 飞机机型
        strcpy(ch_data.startcity, ptr->data.startcity);   // 起点站
        strcpy(ch_data.arrivecity, ptr->data.arrivecity); // 终点站
        for (size_t i = 0; i < 2; i++)                    // 起飞时间
        {
            ch_data.starttime[i] = ptr->data.starttime[i];
        }
        for (size_t i = 0; i < 2; i++) // 到达时间
        {
            ch_data.arrivetime[i] = ptr->data.arrivetime[i];
        }
        strcpy(ch_data.flytime, ptr->data.flytime); // 飞行时间
        ch_data.value = ptr->data.value;            // 票价
        ch_data.maxNUM = ptr->data.maxNUM;          // 额定载客量
        ch_data.whetherfly = ptr->data.whetherfly;  // 是否起飞
        D_List_delete(Flight_list, ch_id);
        D_List_insert(Flight_list, ch_data, ch_id);
        D_List_file_print(Flight_list);
        break;
    case 8:
        // 是否飞
        D_List_print(Flight_list);
        if (ptr->data.whetherfly == 'y')
        {
            printf("该飞机已经准备起飞,无法修改\n");
            break;
        }
        printf("是否改为已经准备起飞(y/n)\n");
        scanf("%c", &ch_data.whetherfly);
        ch_data.id = ptr->data.id;                        // 航班班次
        strcpy(ch_data.flighttype, ptr->data.flighttype); // 飞机机型
        strcpy(ch_data.startcity, ptr->data.startcity);   // 起点站
        strcpy(ch_data.arrivecity, ptr->data.arrivecity); // 终点站
        for (size_t i = 0; i < 2; i++)                    // 起飞时间
        {
            ch_data.starttime[i] = ptr->data.starttime[i];
        }
        for (size_t i = 0; i < 2; i++) // 到达时间
        {
            ch_data.arrivetime[i] = ptr->data.arrivetime[i];
        }
        strcpy(ch_data.flytime, ptr->data.flytime);  // 飞行时间
        ch_data.value = ptr->data.value;             // 票价
        ch_data.maxNUM = ptr->data.maxNUM;           // 额定载客量
        ch_data.leftbuyersa = ptr->data.leftbuyersa; // 剩余座位数量
        D_List_delete(Flight_list, ch_id);
        D_List_insert(Flight_list, ch_data, ch_id);
        D_List_file_print(Flight_list);
        break;
    default:
        break;
    }
}
